package com.lkq;

import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class FlowSumReducer extends Reducer<Text, FlowBean, Text, FlowBean>{

    //框架每传递一组数据<手机号,{flowbean,flowbean,flowbean...}>调用一次我们的reduce方法
    //reduce中的业务逻辑就是遍历values，然后累加求和再输出
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context)
            throws IOException, InterruptedException {
        //上行流量计数器和下行流量计数器
        long up_flow_counter = 0;
        long down_flow_counter = 0;

        //上行流量和下行流量累加求和
        for(FlowBean bean : values){
            up_flow_counter += bean.getUpFlow();
            down_flow_counter += bean.getDownFlow();
        }

        //将结果输出
        context.write(key, new FlowBean(key.toString(), up_flow_counter, down_flow_counter));
    }

}